package com.ctshk.app.hotel.controller;

import com.ctshk.common.annotations.OperationLogAnnotation;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.OperationLogModel;
import com.ctshk.common.enums.OperationLogType;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.model.AppReq;
import com.ctshk.common.req.pay.NotifyConsumeReq;
import com.ctshk.common.utils.DateUtil;
import com.ctshk.common.web.support.BaseController;
import com.ctshk.rpc.hotel.dto.h5.HotelRoomDTO;
import com.ctshk.rpc.hotel.req.h5.RoomDetailsQueryH5Req;
import com.ctshk.rpc.hotel.service.IHotelH5Service;
import com.ctshk.rpc.order.hotel.dto.h5.HotelOrderDTO;
import com.ctshk.rpc.order.hotel.dto.h5.HotelOrderInfoDTO;
import com.ctshk.rpc.order.hotel.dto.h5.HotelOrderPointsDTO;
import com.ctshk.rpc.order.hotel.dto.h5.HotelPayDTO;
import com.ctshk.rpc.order.hotel.req.h5.HotelCancelOrderReq;
import com.ctshk.rpc.order.hotel.req.h5.HotelOrderPayReq;
import com.ctshk.rpc.order.hotel.req.h5.HotelOrderPointsReq;
import com.ctshk.rpc.order.hotel.req.h5.HotelOrderSubmitReq;
import com.ctshk.rpc.order.hotel.service.IHotelOrderH5Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

/**
 * @description 酒店订单-H5
 * @author 谢诗宏
 * @date 2021/5/6
 */
@RestController
@Api(value = "酒店订单", tags = "订单")
@RequestMapping("/h5/order")
public class H5HotelOrderController extends BaseController {

    @DubboReference
    private IHotelH5Service hotelH5Service;
    @DubboReference
    private IHotelOrderH5Service hotelOrderH5Service;

    @ApiOperation(value="房间详情-h5/pc", notes="房间详情-h5/pc")
    @PostMapping("/room/detail")
    public Result<HotelRoomDTO> details(@RequestBody @Valid RoomDetailsQueryH5Req req) {
        int days = DateUtil.daysBetween(req.getCheckInDate(), req.getCheckOutDate());
        if (days == 0) {
            return Result.failed(SystemError.HOTEL_10099);
        } else if (days > 28) {
            return Result.failed(SystemError.HOTEL_10100);
        }
        return Result.success(hotelH5Service.detailRoom(req));
    }

    @ApiOperation(value="获取积分-h5/pc", notes="获取当前订单可使用与可获得星赏会积分-h5/pc")
    @PostMapping("/start-vip/points")
    public Result<HotelOrderPointsDTO> star(@RequestBody @Valid HotelOrderPointsReq req) {
        return Result.success(hotelOrderH5Service.starvipPoints(req, getCurrentUserId()));
    }

    @ApiOperation(value="提交订单-h5/pc", notes="提交酒店订单-h5/pc")
    @PostMapping("/submit")
    public Result<HotelOrderDTO> submit(@RequestBody @Valid HotelOrderSubmitReq req, @ApiIgnore AppReq appReq) {
        int days = DateUtil.daysBetween(req.getCheckInDate(), req.getCheckOutDate());
        if (days == 0) {
            return Result.failed(SystemError.HOTEL_10099);
        } else if (days > 28) {
            return Result.failed(SystemError.HOTEL_10100);
        }
        req.setUserId(getCurrentUserAllowNotLogInUserId());
        return Result.success(hotelOrderH5Service.submit(req, appReq));
    }

    @ApiOperation(value="支付完成-h5/pc", notes="酒店订单支付完成后请求此接口获取相关订单信息-h5/pc")
    @GetMapping("/payment/finish/{id}")
    @ApiImplicitParam(name = "id", value = "订单id", required = true)
    public Result<HotelPayDTO> pay(@PathVariable Long id) {
        return Result.success(hotelOrderH5Service.paymentFinish(id));
    }

    @ApiOperation(value="订单详情-h5/pc", notes="订单详情-h5/pc")
    @GetMapping("/info/{id}")
    @ApiImplicitParam(name = "id", value = "订单id", required = true)
    public Result<HotelOrderInfoDTO> info(@PathVariable Long id) {
        return Result.success(hotelOrderH5Service.detail(id));
    }

    @ApiOperation(value="取消订单-h5/pc", notes="取消订单-h5/pc")
    @PostMapping("/cancel")
    public Result cancel(@RequestBody @Valid HotelCancelOrderReq req) {
        return Result.success(hotelOrderH5Service.cancel(req));
    }

}
